﻿@model TimeTracker.Web.Models.EntriesReportModel

@{
    ViewBag.Title = "Index";
}

<h1>Task tracker</h1>

@{ var entriesPerDay = Model.EntriesPerDay; }

<!-- For what to show here check: https://www.toggl.com/tasks --> 
<h2>What have you done today?</h2>
@Html.Action("Create", "Entry")    

<h2>Stuff you've done so far</h2>
<div id="entryList">
    @foreach (var dailyEntries in entriesPerDay) {
        <div class="dateSummary" >
            <h1>@dailyEntries.Date.ToShortDateString()</h1>
            <h2>@dailyEntries.TotalElapsedTime.ToString("hh\\:mm\\:ss") h</h2>
        </div>
                      
        foreach (var entry in dailyEntries.Entries) {
            @Html.DisplayFor(m => entry)
        }    
    }
</div>

<script type="text/javascript">
    $(document).ready(function () {
        $(".entry > form > .buttons > .createButton").live('click', function () {
            var form = $(this).parent().parent();
            var container = $(this).parent().parent().parent();

            $.ajax({
                type: "POST",
                cache: false,
                url: "@Url.Action("create", "entry")",
                data: form.serialize(),
                dataType: "html",
                success: function (response) {
                    if (response == "Ok") {
                        location.reload();
                    } else {
                        container.replaceWith(response);
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) { alert(errorThrown); },
                complete: function () { }
            });
        });

        $(".entry > form > .buttons > .saveButton").live('click', function () {
            var form = $(this).parent().parent();
            var container = $(this).parent().parent().parent();

            $.ajax({
                type: "POST",
                cache: false,
                url: "@Url.Action("edit", "entry")",
                data: form.serialize(),
                dataType: "html",
                success: function (response) {
                    if (response == "Ok") {
                        location.reload();
                    } else {
                        container.replaceWith(response);
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) { alert(errorThrown); },
                complete: function () { }
            });
        });
        
        $(".entry > form > .buttons > .cancelButton").live('click', function () {
            var container = $(this).parent().parent().parent();
            var id = container.attr("id");

            $.ajax({
                type: "GET",
                cache: false,
                url: "@Url.Action("view", "entry")" + id,
                dataType: "html",
                success: function (response) {
                    container.replaceWith(response);
                },
                error: function (jqXHR, textStatus, errorThrown) { alert(errorThrown); },
                complete: function () { }
            });
        });

        $(".entry > .editButton").live('click', function () {
            var container = $(this).parent();
            var id = $(container).attr("id");

            $.ajax({
                type: "GET",
                cache: false,
                url: "@Url.Action("edit", "entry")" + id,
                dataType: "html",
                success: function (response) {
                    container.replaceWith(response);
                },
                error: function (jqXHR, textStatus, errorThrown) { alert(errorThrown); },
                complete: function () { }
            });
        });

        $(".entry > .buttons > .continueButton").live('click', function () {
            var container = $(this).parent().parent();
            var id = $(container).attr("id");

            $.ajax({
                type: "GET",
                cache: false,
                url: "@Url.Action("continue", "entry")" + id,
                dataType: "html",
                success: function (response) {
                    location.reload();
                },
                error: function (jqXHR, textStatus, errorThrown) { alert(errorThrown); },
                complete: function () { }
            });
        });

        $(".entry > .buttons > .stopButton").live('click', function () {
            var container = $(this).parent().parent();
            var id = $(container).attr("id");

            $.ajax({
                type: "GET",
                cache: false,
                url: "@Url.Action("stop", "entry")" + id,
                dataType: "html",
                success: function (response) {
                    location.reload();
                },
                error: function (jqXHR, textStatus, errorThrown) { alert(errorThrown); },
                complete: function () { }
            });
        });
        
    });
</script>

